package com.ohimy.action;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.ohimy.entity.News;
import com.ohimy.entity.NewsSort;
import com.ohimy.service.impl.NewsService;

@Controller("newsAction")
public class NewsAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	@Resource(name = "newsService")
	private NewsService newsService;

	private NewsSort newsSort;
	private News news;
	private List<NewsSort> newsSortList;
	private List<News> newsList;
	private String page_index, maxResult; // 当前页，总数

	/**
	 * 显示所有的栏目列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String sortShow() throws Exception {
		newsSortList = newsService.getAllNewsSort();
		result = "/WEB-INF/site/news/news_sort.jsp";
		return "ohimy";
	}

	/**
	 * 新添栏目
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addSort() throws Exception {
		newsSort.setCreated(new Date());
		newsService.insertNewsSort(newsSort);
		return null;
	}

	/**
	 * 更新栏目
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateSort() throws Exception {
		newsSort.setId(id);
		newsService.updateNewsSort(newsSort);
		return null;
	}

	/**
	 * 删除栏目
	 * 
	 * @return
	 * @throws Exception
	 */
	public String deleteSort() throws Exception {
		System.out.println(id);
		newsService.deleteNewsSort(id);
		return null;
	}

	// ///////////////////////////////
	/**
	 * 显示所有的新闻列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String newsShow() throws Exception {
		int page_index = Integer.parseInt(this.page_index);
		int maxResult = Integer.parseInt(this.maxResult);
		newsList = newsService.getAllNewsByPager(page_index * maxResult,
				maxResult);
		result = "/WEB-INF/site/ajax/news_list.jsp";
		return "ohimy";
	}

	/**
	 * 查看新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String seeNews() throws Exception {
		news = newsService.getNews(id);
		result = "/WEB-INF/site/ajax/news_show.jsp";
		return "ohimy";
	}

	/**
	 * 新添新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String addNews() throws Exception {
		// 当时间项未选择时，即为当前时间
		if (null == news.getCreated() || "".equals(news.getCreated()))
			news.setCreated(new Date());
		news.setNewsSort(newsService.getNewsSort(newsSort.getId()));
		newsService.insertNews(news);
		return null;
	}

	/**
	 * 更新新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateNews() throws Exception {
		news.setId(id);
		newsService.updateNews(news);
		return null;
	}

	/**
	 * 查找新闻以更新
	 * 
	 * @return
	 * @throws Exception
	 */
	public String gotoNews() throws Exception {
		news = newsService.getNews(id);
		newsSortList = newsService.getAllNewsSort();
		result = "/WEB-INF/site/ajax/news_merge.jsp";
		return "ohimy";
	}

	/**
	 * 删除新闻
	 * 
	 * @return
	 * @throws Exception
	 */
	public String deleteNews() throws Exception {
		newsService.deleteNews(id);
		return null;
	}

	/* **************************************** */
	public void setNewsService(NewsService newsService) {
		this.newsService = newsService;
	}

	public List<NewsSort> getNewsSortList() {
		return newsSortList;
	}

	public void setNewsSortList(List<NewsSort> newsSortList) {
		this.newsSortList = newsSortList;
	}

	public NewsSort getNewsSort() {
		return newsSort;
	}

	public void setNewsSort(NewsSort newsSort) {
		this.newsSort = newsSort;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public List<News> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	public String getPage_index() {
		return page_index;
	}

	public void setPage_index(String page_index) {
		this.page_index = page_index;
	}

	public String getMaxResult() {
		return maxResult;
	}

	public void setMaxResult(String maxResult) {
		this.maxResult = maxResult;
	}

}